//
//  KamiNormalVC.m
//  HengYouCai
//
//  Created by lwj on 2017/9/29.
//  Copyright © 2017年 lwj. All rights reserved.
//

#import "KamiNormalVC.h"
#import "KaMiDetailVC.h"
#import "BannerWebViewVC.h"
#import "HengYouCai-Swift.h"

@interface KamiNormalVC ()

@end

@implementation KamiNormalVC

- (void)dealloc{
    NSLog(@"KamiNormalVC dealloc");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.titleName; //@"我的礼品卡";
    self.titles = [NSMutableArray array];
    self.contents = [NSMutableArray array];
    self.titleH = 40;
    self.intervalH = 0;
    [self rightBtn];
    [self getTypeList];
}
- (void)rightBtn
{
    WLGetWeakSelf(weakSelf, self);
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"规则说明" forState:UIControlStateNormal];
    [btn setTitleColor:Normal_333Color forState:UIControlStateNormal];
    btn.frame = CGRectMake(0, 0, 100, 40);
    btn.contentEdgeInsets = UIEdgeInsetsMake(0,0, 0, -5);
    btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        BannerWebViewVC *web = [[BannerWebViewVC alloc] initWithUrlStr:HYC_CdkRole title:@"规则说明"];
        [weakSelf.navigationController pushViewController:web animated:YES withHideBottomBarWhenPush:YES pop:YES];
    }];
    
}
- (void)getTypeList
{
//    [self.view showHUDLoading];
    WLGetWeakSelf(weakSelf, self);
    
    static NSMutableArray *caceshResult = nil;
    if (caceshResult.count) {
        for (NSDictionary *typeDic in caceshResult) {
            
            KaMiDetailVC *couponDetail =  [[KaMiDetailVC alloc] init];
            [weakSelf.contents addObject:couponDetail];
            [weakSelf.titles addObject:typeDic[@"name"]];
            couponDetail.type = typeDic[@"id"];
            couponDetail.imgLink = typeDic[@"icon"];
        }
        [self setUI];
        
    }else{
        
        [self.view showHUDLoading];
        [HYCNetService getMyWalletawardTypeOnSuccess:^(NSDictionary *dictResult) {
            //
            [weakSelf.view hideHUDLoading:0];
            NSArray *arr = dictResult[@"cdkeyTypeList"];
            caceshResult = [[NSMutableArray alloc] initWithArray:arr];
            for (NSDictionary *typeDic in arr) {
                
                KaMiDetailVC *couponDetail =  [[KaMiDetailVC alloc] init];
                [weakSelf.contents addObject:couponDetail];
                [weakSelf.titles addObject:typeDic[@"name"]];
                couponDetail.type = typeDic[@"id"];
                couponDetail.imgLink = typeDic[@"icon"];
            }
            [weakSelf setUI];
            
        } andFailed:^(NSInteger code, NSString *errorMsg) {
            //
            [weakSelf.view showHUDFailTips:errorMsg hideDelay:kHUDHideDelay];
        }];
        
    }
   
    
}
/**
 初始化界面
 */
- (void)setUI
{
    self.view.backgroundColor = Normal_BackColor;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.pageTitleView];
    [self.view addSubview:self.pageContentView];
}
#pragma PageTitleViewDelegate
- (void)pageTitleView:(PageTitleView *)titleView selectedIndex:(NSInteger)index
{
    [_pageContentView setCurrentIndex:index];
}
#pragma PageContentViewDelegate

- (void)pageContentView:(PageContentView *)contentView progress:(CGFloat)progress sourceIndex:(NSInteger)sourceIndex targetIndex:(NSInteger)targetIndex
{
    [_pageTitleView setTitleWithProgress:progress sourceIndex:sourceIndex targetIndex:targetIndex];
}
#pragma 懒加载
- (PageTitleView *)pageTitleView
{
    if (!_pageTitleView) {
        
        /**
         标题栏
         */
        _pageTitleView = [[PageTitleView alloc] initWithFrame:CGRectMake(0, kJcwGetNavHeight(), f_Device_w, _titleH) titles:self.titles];
        _pageTitleView.delegate = self;
        _pageTitleView.backgroundColor = [UIColor whiteColor];
    }
    return _pageTitleView;
}
- (PageContentView *)pageContentView
{
    if (!_pageContentView) {
        /**
         内容
         */
        CGRect rect = CGRectMake(0, kJcwGetNavHeight()+_titleH+_intervalH, f_Device_w, f_Device_h-(kJcwGetNavHeight()+_titleH+_intervalH));
        
        _pageContentView = [[PageContentView alloc] initWithFrame:rect childVcs:_contents parentViewController:self];
        _pageContentView.delegate = self;
    }
    return _pageContentView;
}

@end
